package com.example.leetcode.trainingcamp.week7.Thursday;

/**
 * 给定一棵二叉树，你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
 */
public class Test543 {

    int result = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) return 0;
        Depth(root);
        return result;
    }


    public int Depth(TreeNode root){
        if (root == null) return 0;
        int leftDepth = Depth(root.left);
        int rightDepth = Depth(root.right);
        result = Math.max(result,leftDepth+rightDepth);
        return Math.max(leftDepth,rightDepth)+1;
    }

}


class Demo543{
  public static void main(String[] args) {
    TreeNode root = new TreeNode(1);
    root.left = new TreeNode(2);
    root.right = new TreeNode(3);
    root.left.left = new TreeNode(4);
    root.left.right = new TreeNode(5);
    Test543 t = new Test543();
    System.out.println(t.diameterOfBinaryTree(root));
  }
}